<%
  game = Game.find(session[:player].game_id)
  game_setting = game.game_setting
  player = Player.find(session[:player].id)
  aps = Account.find(:all, :conditions=>["game_id=? AND debtor_id=? AND trade_year=?", game.id, player.id, game.current_year])
%>
<a href="/user/plan_game">วางแผนการผลิตและการเงิน (B)</a> &nbsp;&nbsp;&nbsp;&nbsp;
<a href="/user/rawmarket">ตลาดซื้อขาย <%=game_setting.inventory_a_name%></a> &nbsp;&nbsp;&nbsp;&nbsp;
<a href="/user/foundgame">ตลาดซื้อขาย <%=game_setting.inventory_b_name%></a>
<br/>
<%if game.a_run_flag=='Y'%>
  <font color="green">คุณสามารถส่งคำสั่งได้</font>
<%else%>
  <font color="red">คุณไม่สามารถส่งคำสั่งได้</font>
<%end%>
<br/>
<center>
<span style="font-size:30px; color:red;"><u><%=@word%></u></span>
</center>
<br/>
Game Session : <%=game.name%> &nbsp;&nbsp;&nbsp;&nbsp; Year: <%=game.current_year%> &nbsp;&nbsp;&nbsp;&nbsp; Round: -
&nbsp;&nbsp;&nbsp;&nbsp;
<%if game.a_run_flag=='N'%>
<img src="/images/stop_icon.jpg" width="50px" style="vertical-align:middle;"/>
<%else%>
<img src="/images/start_icon.gif" width="50px" style="vertical-align:middle;"/>
<%end%>
&nbsp;&nbsp;&nbsp;&nbsp;
<span style="font-size:30px;">Market Size:</span>
<span style="font-size:40px; color:blue;"><%=number_with_delimiter(game.market_size)%></span>
<br/>
<table width="100%">
<tr align="center" valign="top">
  <td width="45%">
    <h3>Bid Order</h3>
    <form action="/user/place_bid" method="post">
    <fieldset>
      <table>
        <tr align="left">
          <td>Price(บาท):</td>
          <td><=<input type="text" name="price" size="10"/></td>
          <td>Credit(ปี):</td>
          <td>
            >=<select name="credit">
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
            </select>
          </td>
        </tr>
        <tr align="left">
          <td>Quantity(ล้านชิ้น):<br/></td>
          <td>=<input type="text" name="quantity" size="10" value="0.00"/></td>
          <td colspan="2"><input type="submit" value="Bid"/></td>
        </tr>
      </table>
    </fieldset>
    </form>
    ราคาของผู้ขายรายใหญ่(บาท):<%=game_setting.unit_cost%>, Credit(ปี):0
    <br/>
    <%
      bids = Trade.find(:all, :conditions=>["bidask_flag=? AND game_id=? AND t_status=? AND player_id=?", "B", game.id, "Openned", player.id])
    %>
    <table width="95%" style="border: 3px solid black; font-size: 18px;">
      <tr align="center" style="border: 3px solid black;" bgcolor="#cccccc">
        <th>Price</th><th>Credit</th><th>Quantity</th><th></th>
      </tr>
      <%bids.each do |b|%>
      <tr align="center">
        <td><%=b.price%></td>
        <td><%=b.credit_term%></td>
        <td><%=number_with_delimiter(b.quantity)%></td>
        <td><a href="/user/remove_bidoffer/<%=b.id%>">X</a></td>
      </tr>
      <%end%>
    </table>
  </td>
  <td>
    <%if game.game_setting.direct_trade=='Y'%>
      <div id="offer_area">
        <%direct_offers = Trade.find(:all, :conditions=>["game_id=? AND direct_player_id=?", game.id, player.id])%>
        <%if direct_offers.size>0%>
          <h3>Direct Offer</h3>
          <table width="95%" style="border: 3px solid black; font-size: 18px;">
            <tr align="center" style="border: 3px solid black;" bgcolor="#cccccc">
              <th>Creditor</th><th>Price</th><th>Credit</th><th>Quantity</th><th></th>
            </tr>
            <%direct_offers.each do |dof|%>
            <tr align="center">
              <td align="left"><%=Player.find(dof.player_id).name%></td>
              <td><%=dof.price%></td>
              <td><%=dof.credit_term%></td>
              <td><%=number_with_delimiter(dof.quantity)%></td>
              <td>
                <a href="/user/accept_direct_offer/<%=dof.id%>">OK</a>
                <a href="/user/reject_direct_offer/<%=dof.id%>">Cancel</a>
              </td>
            </tr>
            <%end%>
          </table>
          <br/>
        <%end%>
      </div>
    <%end%>
    <h3>Traded</h3>
    <table width="95%" style="border: 3px solid black; font-size: 18px;">
      <tr align="center" style="border: 3px solid black;" bgcolor="#cccccc">
        <th>Creditor</th><th>Price</th><th>Credit</th><th>Quantity</th><th>Total</th>
      </tr>
      <%
        sum_quantity = 0
        sum_amount = 0
      %>
      <%aps.each do |ap|%>
      <%
        sum_quantity += ap.quantity
        sum_amount += ap.amount
      %>
      <tr align="center">
        <td align="left"><%=Player.find(ap.creditor_id).name%></td>
        <td><%=ap.price%></td>
        <td><%=ap.credit_term%></td>
        <td><%=number_with_delimiter(ap.quantity)%></td>
        <td><%=number_with_delimiter(ap.amount)%></td>
      </tr>
      <%end%>
      <tr align="center">
        <td colspan="3">Total</td>
        <td><%=number_with_delimiter(sum_quantity)%></td>
        <td><%=number_with_delimiter(sum_amount)%></td>
      </tr>
    </table>
  </td>
</tr>
</table>
<script type="text/javascript">
  function refresh_offer(){
    new Ajax.Updater('offer_area', '/game_runnings/offer_area/<%=player.id%>', {asynchronous:true, evalScripts:true});
  }
  <%if game.a_run_flag=="Y" && game.game_setting.direct_trade=='Y'%>
    window.setInterval(refresh_offer,10000);
  <%end%>
</script>